/**
 * @Title: ExampleFile.java
 * @Package com.example.package
 * @Description: Placeholder (briefly describe the purpose of this file)
 * @Author: Evan
 * @Date: 2023-08-18
 * @Version: V1.0
 *
 * Copyright Notice:
 * This project is released in open source and follows the applicable open source license.
 * All rights reserved. (c) 2023 Evan.
 * This project is protected by copyright laws and international copyright treaties.
 * Unauthorized reproduction, sale, copy, modification, merge, publication, distribution,
 * licensing, and/or sales of this software and its copies are prohibited without permission.
 * Please contact the author for authorization.
 * Contact Information:
 * @Email: uyevan@163.com
 */

package com.evan.biznak.vip.Thread;

import android.util.Log;

import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import org.json.JSONException;
import org.json.JSONObject;

public class GetThread extends Thread {
    /*实参*/
    private final HttpParams params;
    private String url;

    /*成功 失败统计*/
    private int suc = 0;
    private int err = 0;

    public GetThread(HttpParams params, String url) {
        this.params = params;
        this.url = url;
    }

    /*成功*/
    private synchronized void sucNum() {
        suc++;
    }

    /*失败*/
    private synchronized void errNum() {
        err++;
    }

    /*suc获取*/
    public int getSuc() {
        return suc;
    }

    /*err获取*/
    public int getErr() {
        return err;
    }

    @Override
    public void run() {
        EasyHttp.get(this.url)
                .params(this.params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        /*失败内统计*/
                        errNum();
                    }

                    @Override
                    public void onSuccess(String s) {
                        Log.e("===========ThreadData===========", s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject json_result = jsonObject.getJSONObject("result"); // 状态
                            if (json_result.getInt("state") != 300000) {
                                errNum();
                                return;
                            }
                            sucNum();
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
    }
}
